package test_junit_merchant.ProductStock;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import util.BaseDriver;

import java.util.List;

/***
 * Created by Eric on 2018/2/2.
 * 商户-产品类别管理
 */
public class ProductCategory extends BaseDriver {
    private static ProductCategory pc = new ProductCategory();

    public static ProductCategory getInstance() {
        return pc;
    }

    private ProductCategory() {
    }

    public void selectCategory() throws InterruptedException {
        driver.switchTo().defaultContent();
        driver.switchTo().frame(driver.findElement(By.name("iframeundefined")));
        driver.switchTo().frame(driver.findElement(By.id("type_frame")));

        // 产品目录
        driver.findElement(By.xpath("//a[contains(text(), '车载用品（汽车用品）')]/preceding-sibling::i[1]")).click();
        Thread.sleep(1000);
        driver.findElement(By.linkText("其他精品")).click();
        Thread.sleep(1000);
    }

    public void addCategory(String categoryName) throws InterruptedException {
        driver.switchTo().defaultContent();
        driver.switchTo().frame(driver.findElement(By.name("iframeundefined")));
        driver.switchTo().frame(driver.findElement(By.id("table_frame")));

        // 检查该类别是否存在
        int size = driver.findElements(By.xpath("//td[contains(text(), '" + categoryName + "')]/preceding-sibling::td[1]/input")).size();
        if (size > 0) {
            System.err.println("目录名为：" + categoryName + " 的目录已存在");
            return;
        }

        // 点击新增
        driver.findElement(By.xpath("//button[@onclick='add()']")).click();
        Thread.sleep(1000);
        driver.switchTo().frame(0);

        // 名称
        driver.findElement(By.name("name")).sendKeys(categoryName);
        // 排序
        driver.findElement(By.name("seq")).sendKeys("1");

        driver.switchTo().parentFrame();
        driver.findElement(By.linkText("确定")).click();
        Thread.sleep(2000);

        // 检查操作结果
        size = driver.findElements(By.xpath("//td[contains(text(), '" + categoryName + "')]/preceding-sibling::td[1]/input")).size();
        if (size == 1) {
            System.out.printf("添加目录成功，目录名：%s",categoryName);
        } else {
            System.err.printf("添加目录失败，目录名：%s", categoryName);
        }
    }

    public void deleteCategory(String categoryName) throws InterruptedException {
        driver.switchTo().defaultContent();
        driver.switchTo().frame(driver.findElement(By.name("iframeundefined")));
        driver.switchTo().frame(driver.findElement(By.id("type_frame")));

        // 切换到右边内容iframe
        driver.switchTo().parentFrame();
        driver.switchTo().frame(driver.findElement(By.id("table_frame")));

        // 检查该类别是否存在
        List<WebElement> elements = driver.findElements(By.xpath("//td[contains(text(), '" + categoryName + "')]/preceding-sibling::td[1]/input"));
        if (elements.size() == 0) {
            System.err.printf("目录:%s 不存在", categoryName);
            return;
        }

        for (WebElement e : elements) {
            e.click();
            Thread.sleep(500);
        }

        driver.findElement(By.xpath("//button[@onclick='del();']")).click();
        Thread.sleep(1000);

        driver.findElement(By.linkText("确定")).click();
        Thread.sleep(2000);

        // 检查操作结果
        int size = driver.findElements(By.xpath("//td[contains(text(), '" + categoryName + "')]/preceding-sibling::td[1]/input")).size();
        if (size == 0) {
            System.out.printf("删除目录成功；目录名：%s", categoryName);
        } else {
            System.err.printf("删除目录失败；目录名：%s", categoryName);
        }

    }

    public void editCategory(String categoryName, String newCategoryName) throws InterruptedException {
        driver.switchTo().defaultContent();
        driver.switchTo().frame(driver.findElement(By.name("iframeundefined")));
        driver.switchTo().frame(driver.findElement(By.id("type_frame")));

        // 切换到右边内容iframe
        driver.switchTo().parentFrame();
        driver.switchTo().frame(driver.findElement(By.id("table_frame")));

        // 检查该类别是否存在
        List<WebElement> elements = driver.findElements(By.xpath("//td[contains(text(), '" + categoryName + "')]/preceding-sibling::td[1]/input"));
        if (elements.size() == 0) {
            System.err.printf("目录:%s 不存在", categoryName);
            return;
        }

        elements.get(0).click();

        driver.findElement(By.xpath("//button[@onclick='edit()']")).click();
        Thread.sleep(1000);
        driver.switchTo().frame(0);

        // 名称
        driver.findElement(By.name("name")).clear();
        driver.findElement(By.name("name")).sendKeys(newCategoryName);

        // 排序
        driver.findElement(By.name("seq")).clear();
        driver.findElement(By.name("seq")).sendKeys("1");

        driver.switchTo().parentFrame();
        driver.findElement(By.linkText("确定")).click();
        Thread.sleep(2000);

        // 检查操作结果
        int size = driver.findElements(By.xpath("//td[contains(text(), '" + newCategoryName + "')]/preceding-sibling::td[1]/input")).size();
        if (size == 1) {
            System.out.printf("编辑目录成功；新目录名：%s", newCategoryName);
        } else {
            System.err.printf("编辑目录失败；新目录名：%s", newCategoryName);
        }

    }
}
